import java.util.Arrays;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28779
 * Date: 2023-05-20
 * Time: 12:07
 */
public class Sort {
    public static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= 0; j--) {
                //这里加一个等号 就不是一个稳定的排序了
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    //array[j+1] = tmp;
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }


    /**
     * 每组进行直接插入排序
     * @param array
     * @param gap
     */
    private static void shell(int[] array,int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i-gap;
            for (; j >= 0; j -= gap) {
                //这里加一个等号 就不是一个稳定的排序了
                if(array[j] > tmp) {
                    array[j+gap] = array[j];
                }else {
                    //array[j+gap] = tmp;
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    /**
     * 时间复杂度：O(n^1.25 - n^1.5)  -> n^1.3
     * 空间复杂度：O(1)
     * 稳定性：不稳定排序
     * @param array
     */
    public static void shellSort(int[] array) {
        int gap = array.length;//7
        while (gap > 1) {
            gap /= 2;//1
            shell(array,gap);
        }
        //shell(array,1);
    }

    /**
     * 选择排序
     * 时间复杂度：O(n^2) 不管你本身的数据 是有序还是无序  都是这个复杂度
     * 空间复杂度：O(1)
     * 稳定性：不稳定的排序
     * @param array
     */
    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i+1; j < array.length; j++) {
                if(array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array,minIndex,i);
        }
    }

    public static void selectSort2(int[] array) {
        int left = 0;
        int right = array.length-1;
        while (left < right) {
            int minIndex = left;
            int maxIndex = left;
            for (int i = left+1; i <= right; i++) {
                if(array[i] < array[minIndex]) {
                    minIndex = i;
                }
                if(array[i] > array[maxIndex]) {
                    maxIndex = i;
                }
            }
            swap(array,minIndex,left);
            if(maxIndex == left) {
                maxIndex = minIndex;
            }
            swap(array,maxIndex,right);
            left++;
            right--;
        }
    }

    /**
     * 堆排序
     * 时间复杂度：O(n*logn)  对数据不敏感  不管有序无序都是这个表达式
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     * @param array
     */
    public static void heapSort(int[] array) {
        createBigHeap(array);
        int end = array.length-1;
        while (end > 0) {
            swap(array,end,0);
            siftDown(array,0,end);
            end--;
        }
    }

    private static void createBigHeap(int[] array) {
        for (int i = (array.length-1-1) / 2; i >= 0 ; i--) {
            siftDown(array,i,array.length);
        }
    }

    private static void siftDown(int[] array,int parent,int len) {
        int child = 2*parent+1;
        while (child < len) {
            if(child+1 < len && array[child] < array[child+1]) {
                child++;
            }
            if(array[child] > array[parent]) {
                swap(array,child,parent);
                parent = child;
                child = 2*parent+1;
            }else {
                break;
            }
        }
    }

    /**
     * 冒泡排序
     * 时间复杂度：O(N^2) 对数据不敏感  有序 无序都是这个复杂度！
     * 空间负责度：O(1)
     * 稳定性：稳定的排序
     *     插入排序   冒泡排序
     *
     * 加了优化之后，时间复杂度可能会变成O(n)
     * @param array
     */
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length-1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j] > array[j+1]) {
                    swap(array,j,j+1);
                    flg = true;
                }
            }
            if(!flg) {
                break;
            }
        }
    }

    /**
     * 1.hoare法 - 快速排序
     * 时间复杂度：O(n * log n)
     * @param array
     */
    public static void quickSort1(int[] array) {
        quick1(array, 0, array.length - 1);
    }
    private static void quick1(int[] array, int start, int end) {
        if(start >= end) {
            return ;
        }

        int pivot = partition1(array, start, end);

        quick1(array, start, pivot - 1);
        quick1(array, pivot + 1, end);
    }
    private static int partition1(int[] array, int left, int right) {
        int i = left;
        int temp = array[left];
        while (left < right) {
            while(left < right && array[right] >= temp) {
                right --;
            }
            while(left < right && array[left] <= temp) {
                left ++;
            }
            swap(array, left, right);
        }
        swap(array, i, left);
        return left;
    }

    /**
     * 2.挖坑法 - 快速排序
     * @param array
     */

    public static void quickSort2(int[] array) {
        quick2(array, 0, array.length - 1);
    }
    private static void quick2(int[] array, int start, int end) {
        if(start >= end) {
            return ;
        }

        int pivot = partition2(array, start, end);

        quick2(array, start, pivot - 1);
        quick2(array, pivot + 1, end);
    }

    private static int partition2(int[] array, int left, int right) {
        int temp = array[left];
        while (left < right) {
            while (left < right && array[right] >= temp) {
                right --;
            }
            array[left] = array[right];
            while (left < right && array[left] <= temp) {
                left ++ ;
            }
            array[right] = array[left];
        }
        array[right] = temp;
        return left;
    }

    /**
     * 3.前后指针法 - 快速排序
     * @param array
     */

    public static void quickSort3(int[] array) {
        quick3(array, 0, array.length - 1);
    }
    private static void quick3(int[] array, int start, int end) {
        if(start >= end) {
            return ;
        }

        int pivot = partition3(array, start, end);

        quick3(array, start, pivot - 1);
        quick3(array, pivot + 1, end);
    }

    private static int partition3(int[] array, int left , int right) {
        int prev = left;
        int cur = left + 1;
        while (cur <= right) {
            if(array[cur] < array[left] && array[++prev] != array[cur]) {
                swap(array, prev, cur);
            }
            cur ++;
        }
        swap(array, prev, left);
        return prev;
    }


    /**
     * 4.三数取中法 - 快速排序
     * @param array
     */

    public static void quickSort4(int[] array) {
        quick4(array, 0, array.length - 1);
    }
    private static void quick4(int[] array, int start, int end) {
        if(start >= end) {
            return ;
        }
        if(end - start + 1 <= 20) {
            //直接插入排序
            insertSort2(array,start,end);
        }
        int mid = threeNum(array, start, end);
        swap(array, start, mid);
        int pivot = partition4(array, start, end);

        quick4(array, start, pivot - 1);
        quick4(array, pivot + 1, end);
    }
    private static int partition4(int[] array, int left, int right) {
        int temp = array[left];
        while (left < right) {
            while (left < right && array[right] >= temp) {
                right --;
            }
            array[left] = array[right];
            while (left < right && array[left] <= temp) {
                left ++ ;
            }
            array[right] = array[left];
        }
        array[right] = temp;
        return left;
    }
    private static int threeNum(int[] array, int left, int right) {
        int mid = (left + right) / 2;
        if(array[left] > array[right]) {
            if(array[mid] > array[left]) {
                return left;
            } else if (array[mid] < array[right]) {
                return right;
            } else {
                return mid;
            }
        } else {
            if(array[mid] > array[right]) {
                return right;
            } else if (array[mid] < array[left]) {
                return left;
            } else {
                return mid;
            }
        }
    }

    /**
     * 非递归实现 - 快排
     * @param array
     */
    public static void quickSort5(int[] array) {

        Stack<Integer> stack = new Stack<>();
        int start = 0;
        int end = array.length-1;

        if(end - start +1 <= 20) {
            //直接插入排序
            insertSort2(array,start,end);
            return;
        }

        //三数取中
        int mid = threeNum(array,start,end);
        //交换
        swap(array,mid,start);/**/

        int pivot = partition5(array,start,end);
        if(pivot > start+1) {
            stack.push(start);
            stack.push(pivot-1);
        }

        if(pivot < end-1) {
            stack.push(pivot+1);
            stack.push(end);
        }

        while (!stack.empty()) {
            end = stack.pop();
            start = stack.pop();

            if(end - start +1 <= 20) {
                //直接插入排序partition5
                insertSort2(array,start,end);
                //return; 这个地方不能return
            }else {
                mid = threeNum(array,start,end);
                //交换
                swap(array,mid,start);/**/

                pivot = partition5(array, start, end);
                if (pivot > start + 1) {
                    stack.push(start);
                    stack.push(pivot - 1);
                }
                if (pivot < end - 1) {
                    stack.push(pivot + 1);
                    stack.push(end);
                }
            }
        }
    }
    private static int partition5(int[] array, int left, int right) {
        int temp = array[left];
        while (left < right) {
            while (left < right && array[right] >= temp) {
                right --;
            }
            array[left] = array[right];
            while (left < right && array[left] <= temp) {
                left ++ ;
            }
            array[right] = array[left];
        }
        array[right] = temp;
        return left;
    }
    private static int threeNum2(int[] array, int left, int right) {
        int mid = (left + right) / 2;
        if(array[left] > array[right]) {
            if(array[mid] > array[left]) {
                return left;
            } else if (array[mid] < array[right]) {
                return right;
            } else {
                return mid;
            }
        } else {
            if(array[mid] > array[right]) {
                return right;
            } else if (array[mid] < array[left]) {
                return left;
            } else {
                return mid;
            }
        }
    }

    private static void insertSort2(int[] array,int left,int right) {
        for (int i = left+1; i <= right; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= left; j--) {
                //这里加一个等号 就不是一个稳定的排序了
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    //array[j+1] = tmp;
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    private static void swap(int[] array, int index1, int index2) {
        int temp = array[index1];
        array[index1] = array[index2];
        array[index2] = temp;
    }


    public static void main(String[] args) {
        int[] array = {999,100,2,6,1,8,2,50,49,2,8,5,7777,16,6,3,9,55,618,48,6, 848 ,656,131,232};

 /*       System.out.println("======== 1.hoare法 - 快排 ========");
        quickSort1(array);
        System.out.println(Arrays.toString(array));

        System.out.println("======== 2.挖坑法 - 快排 ========");
        quickSort2(array);
        System.out.println(Arrays.toString(array));

        System.out.println("======== 3.前后指针法 - 快排 ========");
        quickSort3(array);
        System.out.println(Arrays.toString(array));

        System.out.println("======== 4.三数取中法 - 快排 ========");
        quickSort4(array);
        System.out.println(Arrays.toString(array));*/

        quickSort5(array);
        System.out.println(Arrays.toString(array));
    }
}
